package ua.shop;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.io.InputStream;

//TODO do happily
public class Connector {

   // private static Logger logger = Logger.getLogger(Connector.class);
    private Document document = null;

    //TODO just funny
    public void connect(String url) throws ConnectionException {
        try {
            InputStream in = Connector.class.getResourceAsStream(url);
            if (in == null) {
                throw new ConnectionException("ConnectionException");
            }
           // logger.debug("current value of Input = " + in);
            document = Jsoup.parse(in, null, "");
            //TODO create test for this branch
           // logger.debug("connect() document has text? = " + document.hasText());

        } catch (IOException e) {
            //TODO create test for this branch
          //  logger.error("IOException");
            throw new ConnectionException("ConnectionException");
        }
    }

    public Document getDocument() throws ConnectionException {
        if(document == null)
            throw new ConnectionException("empty document");
        return document;
    }
}
